<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class Cate extends Model
{
    //关联数据表
    public $table = 'cate';

    //主键
    public $primaryKey = "id";

    //允许批量操作的字段
    public $fillable = ['pid','title','status','order'];

    //不允许批量操作的字段
    public $guarded=[];

    //是否维护crated_at和updated_at字段
    public $timestamps = true;

    //格式化排序
    public function sortCate(){
        $cates = $this->orderBy('order','asc')->get();

        return $this->getTree($cates);
    }

    public function getTree($cate){
        $arr = [];
        foreach ($cate as $v){
            if($v->pid == 0){
                $arr[] = $v;
            }
            foreach ($cate as $n){
                if($n->pid == $v->id){
                    $n->title = '|----'.$n->title;
                    $arr[] = $n;
                }
            }
        }
        return $arr;
    }

}
